<?php
#headers::plain();
header('Content-Type: text/xml');

# Possible URLs for this page
# tags.xml
# images.xml
# albums.xml

#print_r($_GET);

$gallery_url = GALLERY_URL;

$xml = $variable->get('xml', 'string', 'default');
$xmlurls = array();
switch($xml)
{
case 'images':
	$images = $db->arrays("SELECT image_name, image_id, impressions_counter hits FROM gallery_images ORDER BY modified_on DESC LIMIT 100;");
	foreach($images as $i => $image)
	{
		$xmlurls[] = "
<image>
	<caption>{$image['image_name']}</caption>
	<link>{$gallery_url}/image-{$image['image_id']}.jpg</link>
	<hits>{$image['hits']}</hits>
</image>
";
	}
	echo '<?xml version="1.0" encoding="utf-8"?><images>'; echo implode("\r\n", $xmlurls); echo '</images>';
	break;
case 'albums':
	$albums = $db->arrays("SELECT album_name, album_id, images_counter images FROM gallery_albums ORDER BY modified_on DESC LIMIT 100;");
	foreach($albums as $i => $album)
	{
		$xmlurls[] = "
<album>
	<title>{$album['album_name']}</title>
	<link>{$gallery_url}/album-{$album['album_id']}.php</link>
	<images>{$album['images']}</images>
</album>
";
	}
	echo '<?xml version="1.0" encoding="utf-8"?><images>'; echo implode("\r\n", $xmlurls); echo '</images>';
	break;
case 'tags':
	# SELECT tag_name FROM gallery_tags ORDER BY added_on DESC LIMIT 100;
	break;
case 'categories':
	$categories = $db->arrays("SELECT category_name, category_slug, category_id, images_counter images FROM gallery_categories ORDER BY modified_on DESC LIMIT 100;");
	foreach($categories as $c => $category)
	{
		$xmlurls[] = "
<album>
	<title>{$category['category_name']}</title>
	<link>{$gallery_url}/category-{$category['category_slug']}.php</link>
	<images>{$category['images']}</images>
</album>
";
	}
	echo '<?xml version="1.0" encoding="utf-8"?><images>'; echo implode("\r\n", $xmlurls); echo '</images>';
	break;
default:
	echo '<?xml version="1.0" encoding="utf-8"?><error><message>Request not understood</message></error>';
}
?>